  var torrentData = {};
  var shint="Click to download the first torrent on the page...\n";
  var defTitle="Click to download the first torrent on the page...";
  var nullfunc=function(tab) {};
  var temp=/^.*\//;
  var tTrackers=[];
  var atag=/<(\s)*A[^<>]*>/gim;
  var hrefattr=/href(\s)*=(\s)*('([^'\\]*(\\')?)*'|"([^"\\]*(\\")?)*"|(^\s)*)/gim;
  var hrefsrem=/^href(\s)*=(\s)*('|")?/gim;
  var hreferem=/('|")?$/gim;
  var delSite=/[^\/]*$/gim;
  var httpRX=/^http:\/\//gim;


  tTrackers.push(/^http:\/\/[a-zA-Z0-9\.\_\-]*torrents\.ru\/.*/gim );
  function findTracker(url) {
    for (i=0;tTrackers[i];i++)
      if (tTrackers[i].test(url)) {
        localStorage[url]=1;
        return tTrackers[i].down;
      }
    return 0;
  }

  function checkThis(url) {
    return (localStorage[url]!=null)?localStorage[url]:findTracker(url);
  }

  function reccurent(url,r,title,mainurl,arr) {
    if (r<3) {
    //application/x-bittorrent
    //text/html
    if (checkThis(url)) {
      var xhr = new XMLHttpRequest();
      var sitesneed={prevarr: [], curarr: [], nextarrs: []};
      sitesneed.prevarr=arr;
      function checkAll() {
        function getMainParent() {
          var x=sitesneed;
          while (x.prevarr) x=x.prevarr;
          return x;
        }
        var m=getMainParent();
        function recur(p) {
          
        }
        if (recur(m)) {
        }
      }
      arr.nextarrs.push({url: url, arr: arr, result: ""});
      xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
          if (xhr.status==200) {
            if (xhr.getResponseHeader("Content-Type")=="text/html") {
               var p=xhr.responseText;
               var atags=p.match(atag);
               var t, atagsn=[];
               for (var x in atags) 
                 if (t=atags[x].match(hrefattr)) {
                   if ((t[0]=t[0].replace(hrefsrem,"").replace(hreferem,"")).search(httpRX)<0)
                     t[0]=mainurl+t[0]; 
                   if (checkThis(t[0])) atagsn.push(t[0]);
               }  
               for (var x in atags) atags.pop();
               atags=null;             
               for (var x in atagsn) if (url!=atagsn[x]) reccurent(atagsn[x],r+1,title,atagsn[x].replace(delSite,""),sitesneed);
               
               for (var x in arr.nextarrs)
                 if (arr.nextarrs[x].url==url)
            }
            if (xhr.getResponseHeader("Content-Type")=="application/x-bittorrent") {
               arr[url]='yes';
            }
          } else if (sitesneed.prevarr) sitesneed.prevarr[url]=null;          
          checkAll();
        } 
      }
      xhr.open("GET", url, true);
      xhr.send(null);
    }
    }
  }
  function t_onClicked(tab) {
    var t1={url: torrentData[tab.id].url};
    chrome.tabs.update(tab.id,t1,nullfunc);
    if (t1)
    t1.url=null;

    t1=null;   
  }

  function t_onRemoved(reply) {
      if (torrentData[reply.tabId])
      torrentData[reply.tabId].url = null; 
      torrentData[reply.tabId] = null;
  }

  function t_onConnect(port) {
    var tab = port.sender.tab;
    var tempf= function(url) {
      if (url) {
          res=reccurent(url[0],0,null,(url[3].replace(delSite,"")),{prevarr: [], curarr: [], nextarrs: []});
          var t=[res,url[1],url[2]];
//          chrome.tabs.sendRequest(tab.id,t);
      }
    };
	                       
    port.onMessage.addListener(tempf);
  }
  function init() {
    chrome.extension.onConnect.addListener(t_onConnect);
    chrome.pageAction.onClicked.addListener(t_onClicked);
    chrome.tabs.onRemoved.addListener(t_onRemoved);
  }